
//
//  Controller12.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/7.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

// LoginAnimation
class Controller12: UIViewController {
    
    static let tf_w = SCREEN_W - 40;
    
    let nameTF = UITextField(frame: CGRect(x: 0 - tf_w , y: 80, width: tf_w, height: 40));
    
    let psdTF = UITextField(frame: CGRect(x: 0 - tf_w, y: 135, width: tf_w, height: 40));
    
    let loginBtn = UIButton(frame: CGRect(x: -250, y: 200, width: 250, height: 40))


    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        
        var r1 = nameTF.frame;
        r1 = r1.offsetBy(dx:Controller12.tf_w + 20, dy: 0);
        var r2 = psdTF.frame;
        r2 = r2.offsetBy(dx:Controller12.tf_w + 20, dy: 0);

        var r3 = loginBtn.frame;
        r3 = r3.offsetBy(dx: 250 + (SCREEN_W * 0.5 - 120), dy: 0);
        
        UIView.animate(withDuration: 0.5) {
            self.nameTF.frame = r1;
            self.psdTF.frame = r2;
            self.loginBtn.frame = r3;
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green;
        configUI();
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white];
    }
    
    func configUI(){
        
        nameTF.placeholder = "username"
        psdTF.placeholder = "password"
        configTextField(tf: nameTF);
        configTextField(tf: psdTF);

        view.addSubview(loginBtn);
        loginBtn.setTitle("login", for: .normal);
        loginBtn.setTitleColor(.white, for: .normal)
        loginBtn.backgroundColor = UIColor.gray.withAlphaComponent(0.5);
        loginBtn.addTarget(self, action: #selector(loginAction(btn:)), for: .touchUpInside);
        
    }
    
    func configTextField(tf : UITextField){
        tf.layer.cornerRadius = 5;
        tf.backgroundColor = .white;
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10));
        tf.leftView = view;
        tf.leftViewMode = .always;
        self.view.addSubview(tf);
    }

    func loginAction(btn : UIButton){
        print("login action")
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 10, options: .curveLinear, animations: {
            var rect = btn.frame;
            rect.size.width += 40;
            rect.origin.x -= 20;
            btn.frame = rect;
        }) {
                (yes) in
            if yes {
                UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 10, options: .curveLinear, animations: {
                    var rect = btn.frame;
                    rect.size.width -= 40;
                    rect.origin.x += 20;
                    btn.frame = rect;
                }, completion: nil);
            }
            
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
